﻿Public Class FormRolesFuncionesUsuarios

    Dim classController As New Class_RC_Controller.rc_Mantenimiento()

    Private Sub FormRolesFuncionesUsuarios_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        CargarCombos()
        CargarLogs()
    End Sub

    Private Sub CargarCombos()
        Dim ds As DataTable
        ds = classController.get_Usuario_All(oiml.dbConn, 0, "", "", "", 0)
        With cboUsuario
            .DataSource = ds
            .DisplayMember = "usuario"
            .ValueMember = "codusuario"
        End With

        Dim dsRoles As DataTable
        dsRoles = classController.get_Roles_All(oiml.dbConn, "")
        With cboRoles
            .DataSource = dsRoles
            .DisplayMember = "descripcionRole"
            .ValueMember = "codRole"
        End With

    End Sub

    Private Sub CargarLogs()

        Dim codUsuario As Integer = 0
        If cbxUsuario.Checked Then
            codUsuario = cboUsuario.SelectedValue
        End If

        Dim codRol As Integer = 0
        If cbxRol.Checked Then
            codRol = cboRoles.SelectedValue
        End If

        Dim dt As New DataTable
        dt = classController.get_roles_usuario(oiml.dbConn, codUsuario, 0)

        listRolesNo.DataSource = dt
        listRolesNo.DisplayMember = "descripcionRole"
        listRolesNo.ValueMember = "codRole"

        Dim dtSI As New DataTable
        dtSI = classController.get_roles_usuario(oiml.dbConn, codUsuario, 1)

        listRolesSi.DataSource = dtSI
        listRolesSi.DisplayMember = "descripcionRole"
        listRolesSi.ValueMember = "codRole"

        '---------------------------------------------

        Dim dtRolNo As New DataTable
        dtRolNo = classController.get_funciones_rol(oiml.dbConn, codRol, 0)

        listFuncionNo.DataSource = dtRolNo
        listFuncionNo.DisplayMember = "descripcionFuncion"
        listFuncionNo.ValueMember = "codFuncion"

        Dim dtRolSI As New DataTable
        dtRolSI = classController.get_funciones_rol(oiml.dbConn, codRol, 1)

        listFuncionSi.DataSource = dtRolSI
        listFuncionSi.DisplayMember = "descripcionFuncion"
        listFuncionSi.ValueMember = "codFuncion"

        If Not classController.MessageError Is Nothing Then
            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

    End Sub

    Private Sub cboUsuario_SelectedValueChanged(sender As System.Object, e As System.EventArgs) Handles cboUsuario.SelectedValueChanged

        If IsNumeric(cboUsuario.SelectedValue) Then
            CargarLogs()
        End If

    End Sub

    Private Sub cboRoles_SelectedValueChanged(sender As System.Object, e As System.EventArgs) Handles cboRoles.SelectedValueChanged

        If IsNumeric(cboRoles.SelectedValue) Then
            CargarLogs()
        End If

    End Sub

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        Try

            If IsNumeric(cboUsuario.SelectedValue) And IsNumeric(listRolesNo.SelectedValue) Then

                If classController.Insert_Usuario_Rol(oiml.dbConn, cboUsuario.SelectedValue, listRolesNo.SelectedValue) > 0 Then
                    CargarLogs()
                Else

                    If Not classController.MessageError Is Nothing Then
                        MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If

                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnRemove_Click(sender As System.Object, e As System.EventArgs) Handles btnRemove.Click
        Try

            If IsNumeric(cboUsuario.SelectedValue) And IsNumeric(listRolesSi.SelectedValue) Then

                If classController.Delete_Usuario_Rol(oiml.dbConn, cboUsuario.SelectedValue, listRolesSi.SelectedValue) Then
                    CargarLogs()
                Else

                    If Not classController.MessageError Is Nothing Then
                        MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If

                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnAddFuncion_Click(sender As System.Object, e As System.EventArgs) Handles btnAddFuncion.Click
        Try

            If IsNumeric(cboRoles.SelectedValue) And IsNumeric(listFuncionNo.SelectedValue) Then

                If classController.Insert_Rol_Funcion(oiml.dbConn, cboRoles.SelectedValue, listFuncionNo.SelectedValue) > 0 Then
                    CargarLogs()
                Else

                    If Not classController.MessageError Is Nothing Then
                        MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If

                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnRemoveFuncion_Click(sender As System.Object, e As System.EventArgs) Handles btnRemoveFuncion.Click
        Try

            If IsNumeric(cboRoles.SelectedValue) And IsNumeric(listFuncionSi.SelectedValue) Then

                If classController.Delete_Rol_Funcion(oiml.dbConn, cboRoles.SelectedValue, listFuncionSi.SelectedValue) Then
                    CargarLogs()
                Else

                    If Not classController.MessageError Is Nothing Then
                        MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If

                End If

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnAddFuncionAll_Click(sender As System.Object, e As System.EventArgs) Handles btnAddFuncionAll.Click
        Try

            If IsNumeric(cboRoles.SelectedValue) And listFuncionNo.Items.Count > 0 Then

                If MessageBox.Show("Esta seguro de continuar con la configuracion seleccionada?", "Mantenimiento", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
                    Exit Sub
                End If

                Dim dt As DataTable = listFuncionNo.DataSource

                For i As Integer = 0 To dt.Rows.Count - 1
                    If classController.Insert_Rol_Funcion(oiml.dbConn, cboRoles.SelectedValue, dt.Rows(i)("codFuncion")) Then
                        CargarLogs()
                    Else

                        If Not classController.MessageError Is Nothing Then
                            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                            Exit Sub
                        End If

                    End If
                Next

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnRemoveAllFuncion_Click(sender As System.Object, e As System.EventArgs) Handles btnRemoveAllFuncion.Click

        Try

            If IsNumeric(cboRoles.SelectedValue) And listFuncionSi.Items.Count > 0 Then

                If MessageBox.Show("Esta seguro de continuar con la configuracion seleccionada?", "Mantenimiento", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
                    Exit Sub
                End If

                Dim dt As DataTable = listFuncionSi.DataSource

                For i As Integer = 0 To dt.Rows.Count - 1
                    If classController.Delete_Rol_Funcion(oiml.dbConn, cboRoles.SelectedValue, dt.Rows(i)("codFuncion")) Then
                        CargarLogs()
                    Else

                        If Not classController.MessageError Is Nothing Then
                            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                            Exit Sub
                        End If

                    End If
                Next

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

    End Sub

    Private Sub btnAddAll_Click(sender As System.Object, e As System.EventArgs) Handles btnAddAll.Click
        Try

            If IsNumeric(cboUsuario.SelectedValue) And listRolesNo.Items.Count > 0 Then

                If MessageBox.Show("Esta seguro de continuar con la configuracion seleccionada?", "Mantenimiento", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
                    Exit Sub
                End If

                Dim dt As DataTable = listRolesNo.DataSource

                For i As Integer = 0 To dt.Rows.Count - 1
                    If classController.Insert_Usuario_Rol(oiml.dbConn, cboUsuario.SelectedValue, dt.Rows(i)("codRole")) Then
                        CargarLogs()
                    Else

                        If Not classController.MessageError Is Nothing Then
                            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                            Exit Sub
                        End If

                    End If
                Next

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
    End Sub

    Private Sub btnRemoveAll_Click(sender As System.Object, e As System.EventArgs) Handles btnRemoveAll.Click
        Try

            If IsNumeric(cboUsuario.SelectedValue) And listRolesSi.Items.Count > 0 Then

                If MessageBox.Show("Esta seguro de continuar con la configuracion seleccionada?", "Mantenimiento", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No Then
                    Exit Sub
                End If

                Dim dt As DataTable = listRolesSi.DataSource

                For i As Integer = 0 To dt.Rows.Count - 1
                    If classController.Delete_Usuario_Rol(oiml.dbConn, cboUsuario.SelectedValue, dt.Rows(i)("codRole")) Then
                        CargarLogs()
                    Else

                        If Not classController.MessageError Is Nothing Then
                            MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                            Exit Sub
                        End If

                    End If
                Next

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
    End Sub
End Class